//
//  RadioDetailViewController.m
//  MusicPlayer
//
//  Created by dllo on 15/5/29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RadioDetailViewController.h"
#import "SANetWorkingTask.h"
#import "RecommendLatestModel.h"
#import "RankDetailTableViewCell.h"
#import "PlayMusicViewController.h"
#import "OTCover.h"
#import <SDWebImage/UIImageView+WebCache.h>


@interface RadioDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,retain) NSMutableArray *array;

@property (nonatomic, retain) NSMutableArray *playUrlArray;
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) UIImageView * myImageView;;
@property (nonatomic, retain) UIImageView *image1;

@end

@implementation RadioDetailViewController

- (void)dealloc
{
    [_table release];
    [_array release];
    [_playUrlArray release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.array = [NSMutableArray array];
        self.playUrlArray = [NSMutableArray array];
        
        
    }
    return self;
}


-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    [self.myImageView removeFromSuperview];
    
    //    self.navigationItem.title = @"音乐库";
    //    self.navigationController.navigationBar.translucent = NO;
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    //
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //
    
    NSUserDefaults *aa = [NSUserDefaults standardUserDefaults];
    
    
    if ([aa objectForKey:@"pic1"]) {
        
        //    if (aa) {
        UIImage *image = [UIImage imageNamed:[aa objectForKey:@"pic1"]];
        
        [_myImageView setImage:image];
    } else {
        [_myImageView setImage:[UIImage imageNamed:@"PIC2.png"]];
        
    }
    
    [self.view addSubview:_myImageView];
    [self.view bringSubviewToFront:self.table];
    [self.view bringSubviewToFront:self.image1];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.title = self.title;
    UIBarButtonItem *exits = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(exitsAction:)];
    self.navigationItem.leftBarButtonItem = exits;
    
    self.image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/667*self.view.frame.size.height/667.0* 200)];
    [_image1 sd_setImageWithURL:[NSURL URLWithString:self.pic]];
    [self.view addSubview:_image1];
    
    
//    OTCover *test = [[OTCover alloc] initWithTableViewWithHeaderImage:self.pic   withOTCoverHeight:200];
//    test.tableView.delegate = self;
//    test.tableView.dataSource = self;
//    [self.view addSubview:test];

    
    [SANetWorkingTask request:self.webId parmater:nil block:^(NSDictionary *result) {
        
        
        NSArray *array = [result objectForKey:@"data"];
        
        NSDictionary *dic2 = [[NSDictionary alloc] init];
        
        if (array.count > 0) {
            
            for (NSDictionary *dic in array) {
                RecommendLatestModel  *rankDetailModel = [[RecommendLatestModel alloc] init];
                [rankDetailModel setValuesForKeysWithDictionary:dic];
                
                
                if (rankDetailModel.url_list.count > 2) {
                    
                    dic2 = rankDetailModel.url_list[1];
                    NSString *url = [dic2 objectForKey:@"url"];
                    [self.playUrlArray addObject:url];
                    [self.array addObject:rankDetailModel];
                    
                }
            }
            
            
        }
        [self.table reloadData];
    }];
    
    
    [self creatTableView];

}

#pragma mark - 返回图片的事件
- (void)exitsAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)creatTableView
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0,  self.view.frame.size.height/667*200, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height/667.0*self.view.frame.size.height/667.0*200-self.view.frame.size.height/667.0*64-self.view.frame.size.height/667.0*70) style:UITableViewStylePlain];
    self.table.backgroundColor = [UIColor clearColor];
    _table.rowHeight = self.view.frame.size.height/667.0*80;
    _table.delegate =self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    [_table release];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cell";
    RankDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[RankDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease
                ];
    }
  
    cell.backgroundColor = [UIColor clearColor];
    RecommendLatestModel *detailModel = [self.array objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.nameLabel.text = detailModel.song_name;
    cell.nameLabel.textColor = [UIColor whiteColor];
    cell.songLabel.text = detailModel.singer_name;
    cell.songLabel.textColor = [UIColor whiteColor];
    cell.songLabel.font = [UIFont systemFontOfSize:self.view.frame.size.width/375.0*12];
    
    cell.num.text = [NSString stringWithFormat:@"%ld.", indexPath.row+ 1];
    cell.num.textColor = [UIColor whiteColor];

    
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    RecommendLatestModel *detail = [self.array objectAtIndex:indexPath.row];
    NSString *url = [self.playUrlArray objectAtIndex:indexPath.row];
    
    PlayMusicViewController *playMusic = [[PlayMusicViewController alloc ]init];
    
    playMusic.songName = detail.song_name;
    playMusic.singer = detail.singer_name;
    playMusic.songUrl = url;
    playMusic.songArray = self.playUrlArray;
    playMusic.songListArray = self.array;
    playMusic.num = indexPath.row;
    
    [self.navigationController pushViewController:playMusic animated:YES];
    [playMusic release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
